import 'package:cher_flare/http/http_config.dart';
import 'package:cher_flare/modules/login/controllers/ly_login_controller.dart';
import 'package:cher_flare/modules/login/views/ly_login_alert_page.dart';
import 'package:cher_flare/routes/app_pages.dart';
import 'package:cher_flare/utils/app_color.dart';
import 'package:cher_flare/utils/utils.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../utils/ly_use_log_manager.dart';

class LYLoginPage extends StatefulWidget {
  const LYLoginPage({super.key});

  @override
  State<LYLoginPage> createState() => _LYLoginPageState();
}

class _LYLoginPageState extends State<LYLoginPage> {
  final controller = Get.put(LYLoginController());
  final TapGestureRecognizer privacyPolicyGestureRecognizer =
      TapGestureRecognizer();
  final TapGestureRecognizer termOfUseGestureRecognizer =
      TapGestureRecognizer();
  bool isChecked = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    privacyPolicyGestureRecognizer.onTap = () {
      Get.toNamed(AppPages.webView, arguments: {
        'urlString': HttpConfig.privacyPolicy,
        'title': 'Privacy Policy',
        'type': 0
      });
    };
    termOfUseGestureRecognizer.onTap = () {
      Get.toNamed(AppPages.webView, arguments: {
        'urlString': HttpConfig.termConditions,
        'title': 'Term of Use',
        'type': 1
      });
    };
  }

  void _fastLoginDidClick() {
    if (!isChecked) {
      Get.dialog(
          const LYLoginAlertPage(
            isFastLogin: true,
          ),
          useSafeArea: false,
          barrierColor: colorWithRGBA(r: 0, g: 0, b: 0, a: 0.6));
      return;
    }
    controller.login(true);
  }

  void _appleLoginDidClick() {
    if (!isChecked) {
      Get.dialog(
          const LYLoginAlertPage(
            isFastLogin: false,
          ),
          useSafeArea: false,
          barrierColor: colorWithRGBA(r: 0, g: 0, b: 0, a: 0.6));
      return;
    }
    controller.appleLogin();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
            image: AssetImage('assets/images/login_bg_img.png'),
            fit: BoxFit.cover),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset(
            'assets/images/rabbit_img.png',
            width: 207,
          ),
          Container(
            width: double.infinity,
            margin: const EdgeInsets.symmetric(horizontal: 30),
            decoration: const BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/images/login_border_img.png'),
                    fit: BoxFit.fill)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(
                  height: 10,
                ),
                Image.asset(
                  'assets/images/login_welcome_icon.png',
                  height: 88,
                ),
                const SizedBox(
                  height: 20,
                ),
                _makeFastLoginButton(),
                const SizedBox(
                  height: 30,
                ),
                _makeAppleLoginButton(),
                Padding(
                  padding: const EdgeInsets.all(30.0),
                  child: InkWell(
                    onTap: () {
                      isChecked = !isChecked;
                      setState(() {});
                    },
                    child: Text.rich(TextSpan(children: [
                      WidgetSpan(
                          child: Image.asset(
                        isChecked
                            ? 'assets/images/login_checked_img.png'
                            : 'assets/images/login_unchecked_img.png',
                        width: 16,
                        height: 16,
                      )),
                      const TextSpan(
                          text:
                              'By using our App you agree with our Term of Use and Privacy Policy.',
                          style: TextStyle(
                              fontSize: 12, color: AppColors.color202020)),
                      TextSpan(
                          recognizer: termOfUseGestureRecognizer,
                          text: 'Term of Use',
                          style: const TextStyle(
                              fontSize: 12,
                              color: Color.fromARGB(255, 169, 230, 83))),
                      const TextSpan(
                          text: ' and ',
                          style: TextStyle(
                              fontSize: 12, color: AppColors.color202020)),
                      TextSpan(
                          recognizer: privacyPolicyGestureRecognizer,
                          text: 'Privacy Policy.',
                          style: const TextStyle(
                              fontSize: 12,
                              color: Color.fromARGB(255, 169, 230, 83))),
                    ])),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(
            height: 200,
          ),
        ],
      ),
    );
  }

  Widget _makeFastLoginButton() {
    return InkWell(
      onTap: () => Utils.throttle(_fastLoginDidClick, continueClick: () {}),
      child: Container(
        height: 54,
        margin: const EdgeInsets.symmetric(horizontal: 24),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12),
            gradient: const LinearGradient(
                colors: [AppColors.colorCCFF78, AppColors.color78FF85])),
        child: Stack(
          fit: StackFit.expand,
          alignment: AlignmentDirectional.centerStart,
          children: [
            Positioned(
                left: 30,
                child: Image.asset(
                  'assets/images/fast_login_icon.png',
                  width: 24,
                  height: 24,
                )),
            const Center(
              child: Text(
                'Fast Login',
                style: TextStyle(color: AppColors.color404040, fontSize: 16),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _makeAppleLoginButton() {
    return InkWell(
      onTap: () => Utils.throttle(_appleLoginDidClick, continueClick: () {}),
      child: Container(
        height: 54,
        margin: const EdgeInsets.symmetric(horizontal: 24),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          color: AppColors.color65616E,
        ),
        child: Stack(
          fit: StackFit.expand,
          alignment: AlignmentDirectional.centerStart,
          children: [
            Positioned(
                left: 30,
                child: Image.asset(
                  'assets/images/apple_icon.png',
                  width: 24,
                  height: 24,
                )),
            const Center(
              child: Text(
                'Sign in with Apple',
                style: TextStyle(color: AppColors.whiteColor, fontSize: 16),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
